package com.train.mgr.modules.biz.controller;

import com.alibaba.fastjson2.JSONObject;
import com.train.common.model.PageResult;
import com.train.common.model.R;
import com.train.mgr.config.poi.ExcelUtil;
import com.train.mgr.modules.biz.dto.request.*;
import com.train.mgr.modules.biz.dto.response.CourseCheckUserRespListDTO;
import com.train.mgr.modules.biz.dto.response.CourseUserRespListDTO;
import com.train.mgr.modules.biz.dto.response.NotJoinGroupUserRespListDTO;
import com.train.mgr.modules.biz.service.ICourseUserService;
import com.train.mgr.modules.system.dto.request.groups.Insert;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * 课程学员表 前端控制器
 * @author cxg
 * @date 2025-09-30
 */

@Slf4j
@RequiredArgsConstructor
@RestController
@Api( tags = "课程学员管理[企业端]")
@RequestMapping("/courseUser")
public class CourseUserController {

    private final ICourseUserService courseUserService;


    /**
     * 条件 分页查询
     * @author: cxg
     * @date: 2025-09-30
     */
    @GetMapping("/course_user_list")
    @ApiOperation("课程学员列表")
    public R<PageResult<CourseUserRespListDTO>> courseUserList(CourseUserQueryDTO params) {
        return R.succeed(courseUserService.findCourseUserList(params));
    }

    @GetMapping("/course_check_user_list")
    @ApiOperation("课程待审核学员列表")
    public R<PageResult<CourseCheckUserRespListDTO>> courseCheckUserList(CourseCheckUserQueryDTO params) {
        return R.succeed(courseUserService.findCheckCourseUserList(params));
    }

    @GetMapping("/find_no_group_user_list")
    @ApiOperation("查询学员列表（分组）")
    public R<PageResult<NotJoinGroupUserRespListDTO>> findNoGroupUserList(NoGroupQueryReqDTO params) {
        return R.succeed(courseUserService.findNoGroupUserList(params));
    }

    @PostMapping("/set_users_group")
    @ApiOperation("添加或移除学员分组")
    public R<Boolean> setUsersGroup(@RequestBody @Validated SetUserGroupReqDTO body) {
        Boolean result = courseUserService.setUsersGroup(body);
        return R.succeed(result);
    }


    /**
	 * 新增
     * @author: cxg
     * @date: 2025-09-30
	 */
    @PostMapping("/add")
    @ApiOperation("新增学员")
    public R<Boolean> add(@RequestBody @Validated CourseUserAddDTO body) {
        Boolean result = courseUserService.add(body);
        return R.succeed(result);
    }

    @PostMapping("/user_lable_list")
    @ApiOperation("获取人员标签")
    public R<List<String>> userLableList() {
        return R.succeed(courseUserService.getUserLabelList());
    }

    /**
     * 审核
     * @author: cxg
     * @date: 2025-09-30
     */
    @PostMapping("/batch_review")
    @ApiOperation("批量审核通过和不通过")
    public R<Boolean> batchReview(@RequestBody @Validated BatchIdsDTO body) {
        Boolean result = courseUserService.batchReview(body);
        return R.succeed(result);
    }

	/**
	 * 删除
     * @author: cxg
     * @date: 2025-09-30
	 */
    @DeleteMapping("/delete")
    @ApiOperation("移除学员")
    public R<Integer> delete(@RequestParam(name = "id") Long id) {
          return R.succeed(courseUserService.deleteById(id));
    }

    @ApiOperation("导入前验证")
    @PostMapping("/import_check_excel")
    public R<List<StudentImportDTO>> importCheckExcel(@RequestParam("file") MultipartFile file,
                                                      @RequestParam(name = "courseId", required = true) Long courseId) {
        ExcelUtil<StudentImportDTO> util = new ExcelUtil<>(StudentImportDTO.class);
        List<StudentImportDTO> importList=null;
        try {
            importList = util.importExcel(file.getInputStream());
        } catch (Exception e) {
            throw new RuntimeException("excel导入客户数据解析异常，请联系管理员");
        }
        log.info("导入员工 List:{}", JSONObject.toJSONString(importList));
        return R.succeed(courseUserService.checkExcelData(courseId,importList));
    }


    @ApiOperation("导入保存数据")
    @PostMapping("/import_save_excel")
    public R<Boolean> importSaveExcel(@RequestParam("file") MultipartFile file,
                                      @RequestParam(name = "courseId", required = true) Long courseId) {
        ExcelUtil<StudentImportDTO> util = new ExcelUtil<>(StudentImportDTO.class);
        List<StudentImportDTO> importList=null;
        if(courseId == null || courseId ==0){
            throw new RuntimeException("参数【courseId】必填");
        }
        try {
            importList = util.importExcel(file.getInputStream());
        } catch (Exception e) {
            throw new RuntimeException("excel导入客户数据解析异常，请联系管理员");
        }
        log.info("导入员工 List:{}", JSONObject.toJSONString(importList));
        return R.succeed(courseUserService.saveExcelData(courseId,importList));
    }
}
